﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dapper;
using LibMS.Models;

namespace LibMS.Controllers
{
    [Authorize(Roles = "System Admin, Lib Admin")]
    public class LibRackController : Controller
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        //
        // GET: /LibRack/

        public ActionResult Index()
        {
            int? total;
            string message;
            var data = _unitOfWork.LibRack.QuerySp(out total, out message, StringConstant.TABLE_LIBRACK + StringConstant.SP_BASE_GET);
            if (data == null)//Query error
            {
                ViewData["success"] = false;
                ViewData["message"] = message;
                return View();
            }
            //Success
            var sqlResult = data as List<LibRack> ?? null;
            ViewData["success"] = true;
            ViewData["total"] = total;
            return View(sqlResult);
        }

        public ActionResult RackDetail(int rackId)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@RackId", rackId);
            var data = _unitOfWork.LibRack.GetById(out message, parameters);
            if (data == null)//Query error
            {
                ViewData["success"] = false;
                ViewData["message"] = message;
                return View();
            }
            //Success
            ViewData["success"] = true;
            return View();
        }

        public ActionResult GetDataForAdmin()
        {
            int? total;
            string message;
            var data = _unitOfWork.LibRack.QuerySp(out total, out message, StringConstant.TABLE_LIBRACK + StringConstant.SP_BASE_GET);
            if (data == null)//Query error
            {
                ViewData["success"] = false;
                ViewData["message"] = message;
                return View("AdminListRack");
            }
            //Success
            var sqlResult = data as List<LibRack> ?? null;
            ViewData["success"] = true;
            ViewData["total"] = total;
            return View("AdminListRack", sqlResult);
        }
    }
}
